package com.sl.water.vo;

/**
 * @description:
 * @author: duan
 * @time: 2025-06-19 9:25
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.stream.Collectors;


public abstract class BaseVO {
    /**
     * 单对象转换
     * @param source
     * @param targetClass
     * @param <S>
     * @param <T>
     * @return
     */
    public static <S, T> T convert(S source, Class<T> targetClass) {
        return BeanUtil.copyProperties(source, targetClass);
    }

    /**
     * 列表转换
     * @param sourceList
     * @param targetClass
     * @param <S>
     * @param <T>
     * @return
     */
    public static <S, T> List<T> convertList(List<S> sourceList, Class<T> targetClass) {
        return sourceList.stream()
                .map(source -> convert(source, targetClass))
                .collect(Collectors.toList());
    }

    /**
     * 分页转换
     * @param sourcePage
     * @param targetClass
     * @param <S>
     * @param <T>
     * @return
     */
    public static <S, T> Page<T> convertPage(Page<S> sourcePage, Class<T> targetClass) {
        Page<T> targetPage = new Page<>();
        BeanUtil.copyProperties(sourcePage, targetPage, "records");
        targetPage.setRecords(convertList(sourcePage.getRecords(), targetClass));
        return targetPage;
    }
}

